package com.ls.lserp.modules.custom.utils;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.ls.lserp.common.mapper.JsonMapper;
import com.ls.lserp.common.utils.CacheUtils;
import com.ls.lserp.common.utils.Constants;
import com.ls.lserp.common.utils.SpringContextHolder;
import com.ls.lserp.common.utils.StringUtils;
import com.ls.lserp.modules.custom.dao.CustomDao;
import  com.ls.lserp.modules.order.entity.DesignOption;
import java.util.List;
import java.util.Map;

/**
 * 字典工具类
 * @author LIUPENG
 * @version 2016-10-28
 */
public class DesignOptionUtils {
	
	private static CustomDao customDao = SpringContextHolder.getBean(CustomDao.class);

	//code为键值
	public static final String CACHE_DESIGNOPTION_MAP = "designoptionMap";
	//code-value为键值
	public static final String CACHE_DESIGNOPTION_VALUE_MAP = "designoptionValueMap";
	
    
    /**
      * @Title: getDesignOptionList
      * @Description: 返回定制项目列表
      * @author LIUPENG
      * @date 2016-11-16 下午4:48:36
      *
      * @param code
      * @return
      *
      * @return List<DesignOption> 
      * @throws
      */
    
    public static List<DesignOption> getDesignOptionList(String code){
        
        @SuppressWarnings("unchecked")
        Map<String, List<DesignOption>> designOptionMap = (Map<String, List<DesignOption>>)CacheUtils.get(CACHE_DESIGNOPTION_MAP);
        if (designOptionMap==null){
            designOptionMap = Maps.newHashMap();
            for (DesignOption designOption : customDao.findAllList(new DesignOption())){
                List<DesignOption> designOptionList = designOptionMap.get(designOption.getItemCD());
                if (designOptionList != null){
                    designOptionList.add(designOption);
                }else{
                    designOptionMap.put(designOption.getItemCD(), Lists.newArrayList(designOption));
                }
            }
            CacheUtils.put(CACHE_DESIGNOPTION_MAP, designOptionMap);
        }
        List<DesignOption> designOptionList = designOptionMap.get(code);
        
        if (designOptionList == null){
            designOptionList = Lists.newArrayList();
        }else{
            for (DesignOption designOption :designOptionList){
                if(Constants.EN_US.equals(StringUtils.getLanguage())){
                    designOption.setItemName(designOption.getItemNameEN());
                }else{
                    designOption.setItemName(designOption.getItemNameCN());
                }
            }
        }
        
        return designOptionList;
    }
    /**
     * @Title: getDesignOptionList
     * @Description: 返回字典Entity
     * @author liupeng
     * @date 2016-10-10 下午4:48:36
     *
     * @param code
     * @return
     *
     * @return List<DesignOption> 
     * @throws
     */
   
    public static DesignOption getDesignOptionEntity(String value){
        
        Map<String, DesignOption> designOptionValueMap = (Map<String, DesignOption>)CacheUtils.get(CACHE_DESIGNOPTION_VALUE_MAP);
        
        if (designOptionValueMap==null){
            
            designOptionValueMap = Maps.newHashMap();
            for (DesignOption designOption : customDao.findAllList(new DesignOption())){
                designOptionValueMap.put(designOption.getItemCD()+"-"+designOption.getItemValue(),designOption);
            }
            CacheUtils.put(CACHE_DESIGNOPTION_VALUE_MAP, designOptionValueMap);
            
        }
        
        DesignOption returnDesignOption =designOptionValueMap.get(value);
        
        if(returnDesignOption!=null){
            return returnDesignOption;
        }else{
            return new DesignOption();
        }
         
    }
    /**
     * 返回数据字典名称
     * @param value
     * @return
     */
    public static String getDesignOptionName(String value){
        
        Map<String, DesignOption> designOptionValueMap = (Map<String, DesignOption>)CacheUtils.get(CACHE_DESIGNOPTION_VALUE_MAP);
        
        if (designOptionValueMap==null){
            
            designOptionValueMap = Maps.newHashMap();
            for (DesignOption designOption : customDao.findAllList(new DesignOption())){
                designOptionValueMap.put(designOption.getItemCD()+"-"+designOption.getItemValue(),designOption);
            }
            CacheUtils.put(CACHE_DESIGNOPTION_VALUE_MAP, designOptionValueMap);
            
        }
        
        DesignOption returnDesignOption =designOptionValueMap.get(value);
        
        if(returnDesignOption!=null){
            if(Constants.EN_US.equals(StringUtils.getLanguage())){
                return returnDesignOption.getItemNameEN();
            }else{
                return returnDesignOption.getItemNameCN();
            }
        }else{
            return "";
        }
    }

    /**
      * @Title: getDesignOptionListJson
      * @Description: 返回字典列表（JSON）
      * @author 邱明欣 
      * @date 2016-10-10 下午4:48:10
      *
      * @param code
      * @return
      *
      * @return String 
      * @throws
      */
    
    /**
     * 返回字典列表（JSON）
     * @param type
     * @return
     */
    public static String getDesignOptionListJson(String code){
        return JsonMapper.toJsonString(getDesignOptionList(code));
    }
	

}
